package com.x.uikit.channel.feed;

import android.content.Context;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.x.uikit.link.XLink;
import com.x.uikit.link.XParams;

public abstract class AbstractCard<CardData extends IXFeed.CardData> extends FrameLayout implements XLink.EventObserver, XLink.CommandHandler {
    protected XLink.EventObserver mObserver;

    public AbstractCard(@NonNull Context context) {
        super(context);
    }

    public abstract void onBind(int position, CardData data);

    public void onAttached() {

    }

    public void onDetached() {

    }

    public void setObserver(XLink.EventObserver observer) {
        this.mObserver = observer;
    }

    protected void sendEvent(int eventId) {
        sendEvent(eventId, null, null);
    }

    protected void sendEvent(int eventId, XParams params) {
        sendEvent(eventId, params, null);
    }

    protected void sendEvent(int eventId, XParams params, XParams result) {
        if (mObserver != null) {
            mObserver.onEvent(eventId, params, result);
        }
    }

    @Override
    public boolean handCommand(int commandId, XParams params, XParams result) {
        return false;
    }

    @Override
    public void onEvent(int eventId, XParams params, XParams result) {

    }

    public void bind(int position, CardData data) {
        onBind(position, data);
    }
}
